/*
   Copyright 2016 小新de蜡笔

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "mygetline.h"
#include "extract_exception.h"

#define TEMPLATE "\tat "
#define PREPARE_SIZE 32

static ssize_t _ee_create_buf (char **buf, size_t orig_bufsize, size_t new_bufsize)
{
	char *tmpline = NULL;
	size_t size = orig_bufsize;

	if (orig_bufsize != new_bufsize) {
		if (!orig_bufsize) {
			size = new_bufsize + PREPARE_SIZE;
			*buf = malloc(size);
			if (!*buf) {
				return -ENOMEM;
			}
		} else if (orig_bufsize < new_bufsize) {
			size = new_bufsize + PREPARE_SIZE;
			tmpline = realloc(*buf, size);
			if (!tmpline) {
				return -ENOMEM;
			}
			*buf = tmpline;
		}
	}

	return size;
}

static void _ee_destroy_buf (char **buf)
{
	free(*buf);
	*buf = NULL;
}

static ssize_t _ee_mywrite (int fd, void *buf, size_t n)
{
	ssize_t wrlen = 0, rdsize = n;
	while (1) {
		wrlen = write(fd, buf, rdsize);
		if (wrlen < 0) {
			goto e_write;
		}
		rdsize -= wrlen;
		if (!rdsize) {
			break;
		}
	}

	return n;

e_write:
	return -errno;
}

int extract_exception (const int srcfd, const int destfd)
{
	int err = 0;
	ssize_t size = 0;
	ssize_t last_rdsize = 0, last_size = 0;
	char *line = NULL, *lastline = NULL, *topline = NULL;
	int lastflag = 0, count = 0;
	long i = 0;

	for (i = 1; (size = mygetline(srcfd, &line)) > 0; i++) {
		if (strstr(line, TEMPLATE)) {
			if (!lastflag) {
				if ((err = _ee_create_buf(&topline, 0, PREPARE_SIZE)) < 0) {
					goto e_create_buf;
				}
				snprintf(topline, 32, "No.%d\r\n", ++count);
				if (_ee_mywrite(destfd, topline, strlen(topline)) < 0) {
					goto e_write_topline;
				}
				if (_ee_mywrite(destfd, lastline, last_rdsize) < 0) {
					err = -errno;
					goto e_write;
				}
				_ee_destroy_buf(&topline);
			}
			if ((last_size = _ee_create_buf(&lastline, last_size, size)) < 0) {
				err = last_size;
				goto e_create_buf;
			}
			snprintf(lastline, last_size, "%ld  %s", i, line);
			if (_ee_mywrite(destfd, lastline, strlen(lastline)) < 0) {
				err = -errno;
				goto e_write;
			}
			lastflag = 1;
		} else {
			if (lastflag) {
				lastflag = 0;
				if (_ee_mywrite(destfd, "\r\n\r\n\r\n\r\n\r\n", 10) < 0) {
					err = -errno;
					goto e_write;
				}
			}

			if ((last_size = _ee_create_buf(&lastline, last_size, size)) < 0) {
				err = last_size;
				goto e_create_buf;
			}
			snprintf(lastline, last_size, "%ld  %s", i, line);
			last_rdsize = strlen(lastline);
		}
	}
	if (size < 0) {
		goto e_mygetline;
	}

	_ee_destroy_buf(&topline);
	_ee_destroy_buf(&lastline);
	mygetline_destroy(&line);

	return count;

e_create_buf:
e_write_topline:
e_write:
e_mygetline:
	_ee_destroy_buf(&topline);
	_ee_destroy_buf(&lastline);
	mygetline_destroy(&line);
	return err;
}

